<template>
  <div class="dashboard">
    <section class="dashboard-hero">
      <div class="dashboard-hero__text">
        <span class="dashboard-hero__eyebrow">教师门户</span>
        <h2 class="dashboard-hero__title">欢迎回来，{{ userName }}</h2>
        <p class="dashboard-hero__desc">
          高效管理教学任务与课程安排，关注学生动态，让备课与沟通更轻松。
        </p>
        <div class="dashboard-hero__meta" v-if="heroChips.length">
          <span v-for="chip in heroChips" :key="chip" class="dashboard-chip">{{ chip }}</span>
        </div>
      </div>
      <div class="dashboard-hero__badge">
        <el-icon><UserFilled /></el-icon>
      </div>
    </section>

    <el-card class="dashboard-card" shadow="hover">
      <template #header>
        教师信息
      </template>
      <div class="info-grid">
        <div v-for="item in profileItems" :key="item.label" class="info-grid__item">
          <span class="info-grid__label">{{ item.label }}</span>
          <span class="info-grid__value">{{ item.value }}</span>
        </div>
      </div>
    </el-card>

    <div class="dashboard-grid">
      <el-card
        v-for="item in reminders"
        :key="item.title"
        class="dashboard-card tips-card"
        shadow="hover"
        @click="handleCardClick(item)"
      >
        <div class="tips-card__icon">
          <el-icon><component :is="item.icon" /></el-icon>
        </div>
        <h3 class="tips-card__title">{{ item.title }}</h3>
        <p class="tips-card__desc">{{ item.description }}</p>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { computed, markRaw, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { UserFilled, Calendar, User, Bell } from '@element-plus/icons-vue'
import { getTeacherInfo } from '../api/teacherUser'

// 定义 emit 用于页面跳转
const emit = defineEmits(['page-change'])

const user = JSON.parse(localStorage.getItem('user') || '{}')
const teacher = reactive({ ...user })

// 拉取后端教师信息，若本地存的字段名不一致则做一次规范化
const normalize = (t) => ({
  teacherId: t.teacherId ?? t.id ?? t.teId ?? t.teNo,
  teacherNo: t.teacherNo ?? t.teNo ?? '',
  teacherName: t.teacherName ?? t.teName ?? '',
  phone: t.phone ?? t.tePhone ?? '',
  email: t.email ?? t.teEmail ?? '',
  remark: t.remark ?? t.teRemark ?? ''
})

onMounted(async () => {
  try {
    const id = teacher.teacherId || teacher.id || teacher.teId || teacher.teNo
    if (id) {
      const res = await getTeacherInfo(id)
      if (res && res.data) {
        Object.assign(teacher, normalize(res.data))
        // 同步到本地，便于其它页面复用
        localStorage.setItem('user', JSON.stringify({ ...user, ...teacher }))
      }
    }
  } catch (e) {
    // 忽略错误，保持本地数据展示
  }
})

const userName = computed(() => teacher.teacherName || teacher.teName || '老师')

const heroChips = computed(() => {
  const chips = []
  if (teacher?.teacherNo || teacher?.teNo) chips.push(`工号 · ${teacher.teacherNo || teacher.teNo}`)
  if (teacher?.phone || teacher?.tePhone) chips.push(`联系电话 · ${teacher.phone || teacher.tePhone}`)
  if (teacher?.remark || teacher?.teRemark) chips.push(`${teacher.remark || teacher.teRemark}`)
  return chips
})

const profileItems = computed(() => [
  { label: '工号', value: teacher?.teacherNo || teacher?.teNo || '-' },
  { label: '姓名', value: teacher?.teacherName || teacher?.teName || '-' },
  { label: '手机号', value: teacher?.phone || teacher?.tePhone || '-' },
  { label: '邮箱', value: teacher?.email || teacher?.teacherEmail || teacher?.teEmail || '-' },
  { label: 'QQ', value: teacher?.qq || teacher?.teQq || '-' },
  { label: '备注', value: teacher?.remark || teacher?.teRemark || '暂无备注' }
])

const reminders = [
  {
    title: '今日课程',
    description: '查看今天的课程安排，避免错过上课时间。',
    icon: markRaw(Calendar),
    page: 'teacher-today-courses'
  },
  {
    title: '学生快查',
    description: '快速查询学生信息，查看学生名单与联系方式。',
    icon: markRaw(User),
    page: 'teacher-students'
  },
  {
    title: '待办提醒',
    description: '查看待处理事项，包括学生请假、调课申请等。',
    icon: markRaw(Bell),
    page: 'teacher-request'
  }
]

// 处理卡片点击事件
const handleCardClick = (item) => {
  console.log('卡片被点击:', item)
  
  if (!item.page) {
    ElMessage.warning('该功能暂未开放')
    return
  }
  
  console.log('准备跳转到:', item.page)
  ElMessage.success('正在跳转...')
  emit('page-change', item.page)
}
</script>

<style scoped>
.dashboard {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 60px);
}

.dashboard-hero {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 16px;
  padding: 40px;
  color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.3);
}

.dashboard-hero__text {
  flex: 1;
}

.dashboard-hero__eyebrow {
  font-size: 14px;
  font-weight: 500;
  opacity: 0.9;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.dashboard-hero__title {
  font-size: 32px;
  font-weight: 700;
  margin: 12px 0 16px;
}

.dashboard-hero__desc {
  font-size: 16px;
  opacity: 0.95;
  line-height: 1.6;
  margin-bottom: 16px;
}

.dashboard-hero__meta {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.dashboard-chip {
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.dashboard-hero__badge {
  width: 120px;
  height: 120px;
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(10px);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
  border: 3px solid rgba(255, 255, 255, 0.3);
}

.dashboard-card {
  border-radius: 12px;
  margin-bottom: 24px;
  transition: all 0.3s ease;
}

.dashboard-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.dashboard-card :deep(.el-card__header) {
  background: linear-gradient(135deg, #f5f7fa 0%, #e8ecf1 100%);
  font-weight: 600;
  font-size: 16px;
  color: #2c3e50;
  padding: 16px 20px;
  border-bottom: 2px solid #667eea;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  padding: 10px;
}

.info-grid__item {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
  border-radius: 8px;
  transition: all 0.3s ease;
}

.info-grid__item:hover {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
  transform: translateX(4px);
}

.info-grid__label {
  font-size: 14px;
  color: #909399;
  font-weight: 500;
}

.info-grid__value {
  font-size: 16px;
  color: #2c3e50;
  font-weight: 600;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.tips-card {
  cursor: pointer;
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.tips-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 32px rgba(102, 126, 234, 0.2);
  border-color: #667eea;
}

.tips-card:active {
  transform: translateY(-2px);
}

.tips-card__icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  color: #ffffff;
  margin-bottom: 16px;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.tips-card__title {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 12px;
}

.tips-card__desc {
  font-size: 14px;
  color: #606266;
  line-height: 1.6;
  margin: 0;
}

@media (max-width: 768px) {
  .dashboard-hero {
    flex-direction: column;
    text-align: center;
    padding: 30px 20px;
  }

  .dashboard-hero__title {
    font-size: 24px;
  }

  .dashboard-hero__badge {
    width: 80px;
    height: 80px;
    font-size: 40px;
    margin-top: 20px;
  }

  .dashboard-grid {
    grid-template-columns: 1fr;
  }
}
</style>
